/*
 * RemoteScreenViewer.java
 *
 * Created on 3 Ιουλ 2009, 2:02:40 πμ
 */

/**
 *
 * @author Tritonio
 */

package jrpc;

import java.io.IOException;
import java.io.ObjectOutputStream;
import javax.swing.JOptionPane;

class RemoteScreenViewer extends javax.swing.JFrame {

    /** Creates new form RemoteScreenViewer */
    Connection connection;
    ObjectOutputStream objectOutputStream;
    boolean closed = false;

    public RemoteScreenViewer(Connection connection) {
        this.connection=connection;
        try {
            objectOutputStream = new ObjectOutputStream(connection.output);
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "I/O error. This is probably a bug.", "I/O error",JOptionPane.ERROR_MESSAGE);
            closed=true;
        }
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        canvas1 = new java.awt.Canvas();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        canvas1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                canvas1MousePressed(evt);
            }
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                canvas1MouseReleased(evt);
            }
        });
        canvas1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                canvas1MouseMoved(evt);
            }
        });
        canvas1.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                canvas1KeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                canvas1KeyReleased(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(canvas1, javax.swing.GroupLayout.DEFAULT_SIZE, 800, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(canvas1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        closed = true;
    }//GEN-LAST:event_formWindowClosing

    private void canvas1MouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_canvas1MouseMoved
        try {
            objectOutputStream.writeObject(new MouseOrKeybData(0, (double) evt.getX() / canvas1.getWidth(), (double) evt.getY() / canvas1.getHeight()));
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "I/O error. DISPOSING THE CONTROLS AND SETTING CLOSED=TRUE.", "I/O error",JOptionPane.ERROR_MESSAGE);
            this.dispose();
            closed=true;
        }
    }//GEN-LAST:event_canvas1MouseMoved

    private void canvas1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_canvas1MousePressed
        try {
            objectOutputStream.writeObject(new MouseOrKeybData(1, (double) evt.getX() / canvas1.getWidth(), (double) evt.getY() / canvas1.getHeight(), evt.getButton()));
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "I/O error. DISPOSING THE CONTROLS AND SETTING CLOSED=TRUE.", "I/O error",JOptionPane.ERROR_MESSAGE);
            this.dispose();
            closed=true;
        }
    }//GEN-LAST:event_canvas1MousePressed

    private void canvas1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_canvas1MouseReleased
        try {
            objectOutputStream.writeObject(new MouseOrKeybData(2, (double) evt.getX() / canvas1.getWidth(), (double) evt.getY() / canvas1.getHeight(), evt.getButton()));
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "I/O error. DISPOSING THE CONTROLS AND SETTING CLOSED=TRUE.", "I/O error",JOptionPane.ERROR_MESSAGE);
            this.dispose();
            closed=true;
        }
    }//GEN-LAST:event_canvas1MouseReleased

    private void canvas1KeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_canvas1KeyPressed
        try {
            objectOutputStream.writeObject(new MouseOrKeybData(3, evt.getKeyCode()));
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "I/O error. DISPOSING THE CONTROLS AND SETTING CLOSED=TRUE.", "I/O error",JOptionPane.ERROR_MESSAGE);
            this.dispose();
            closed=true;
        }
    }//GEN-LAST:event_canvas1KeyPressed

    private void canvas1KeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_canvas1KeyReleased
        try {
            objectOutputStream.writeObject(new MouseOrKeybData(4, evt.getKeyCode()));
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "I/O error. DISPOSING THE CONTROLS AND SETTING CLOSED=TRUE.", "I/O error",JOptionPane.ERROR_MESSAGE);
            this.dispose();
            closed=true;
        }
    }//GEN-LAST:event_canvas1KeyReleased

    /**
    * @param args the command line arguments
    *
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new RemoteScreenViewer().setVisible(true);
            }
        });
    }
    */

    // Variables declaration - do not modify//GEN-BEGIN:variables
    java.awt.Canvas canvas1;
    // End of variables declaration//GEN-END:variables

}
